package mylibrary.thread.threadpool;

/**
 * A class extends ThreadPoolExecutor to run beforeExecute, afterExecute and terminated.
 * Main point is execute method.
 * 
 * Log:
 * 		before
		Task executed
		after
		terminated
 */
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutorExtension extends ThreadPoolExecutor {

	public ThreadPoolExecutorExtension(int corePoolSize, int maximumPoolSize,
			long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
	}
	
	public static void main(String[] args) {
		ThreadPoolExecutorExtension pool = new ThreadPoolExecutorExtension(0, 
				Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue());
		pool.runTask();
		pool.shutdown();
	}
	
	public void runTask() {
		this.execute(new Runnable(){
			public void run() {
				System.out.println("Task executed");
			}
		});
	}
	
	public void beforeExecute(Thread t, Runnable r) {
		System.out.println("before");
		super.beforeExecute(t, r);
	}
	
	public void afterExecute(Runnable r, Throwable t) {
		System.out.println("after");
		super.afterExecute(r, t);
	}
	protected void terminated() {
		System.out.println("terminated");
		super.terminated();
	}

}
